<?php
/**
 * Created by PhpStorm.
 * User: sunlight
 * Date: 2018/11/19
 * Time: 14:51
 */

namespace app\admin\controller;

use think\Exception;

class Admin extends BaseController{

    public function add(){
        if (request()->isPost()){
            $data=input('post.');
            $validate=validate('AdminUser');
            if (!$validate->check($data)){
                $this->error($validate->getError());
            }

            $data['password'] = md5($data['password'].'_#sing_ty');
            $data['status'] = 1;

            try{
                $id=model('AdminUser')->add($data);
            }catch (Exception $e){
                $this->error($e->getMessage());
            }

            if($id){
                $this->success('$id='.$id.'的用户新增成功！');
            }else{
                $this->error("error");
            }
        }else{
            return $this->fetch();
        }
    }

    /**
     * 列表
     * @return mixed
     */
    public function show(){
        $data=input('param.');
        $query=http_build_query($data);

        $whereData = [];
        //转换查询调教
        if (!empty($data['start_time']) && !empty($data['end_time'])
            && $data['end_time'] > $data['start_time']){
            $whereData['create_time'] = [
                ['gt',strtotime($data['start_time'])],
                ['lt',strtotime($data['end_time'])]
            ];
        }

        //获取数据，然后填充到模板中
        $this->getPageAndSize($data);

        $m=model('AdminUser');

        //获取数据库表数据
        $adminUser=$m->getProductByCondition($whereData, $this->from, $this->size);

        //获取满足条件的数据总数=》有多少页
        $total=$m->getProductCountByCondition($whereData);
        // 结合总数+size  =》 有多少页
        $pageTotal = ceil($total/$this->size);//1.1 =>2

        return $this->fetch('',[
            'adminUser' => $adminUser,
            'pageTotal' => $pageTotal,
            'curr' => $this->page,
            'start_time' => empty($data['start_time'])?'':$data['start_time'],
            'end_time' => empty($data['end_time']) ? '' : $data['end_time'],
            'query' => $query
        ]);
    }

}